package com.imusic;

import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class AccountApplication {
  private static final Logger log = LoggerFactory.getLogger(AccountApplication.class);

  public static void main(String[] args) throws Exception {
    SpringApplication.run(AccountApplication.class, args);
  }

  @RequestMapping("/getAccount")
  public String getAccount() throws InterruptedException {
    Thread.sleep(2000);
    log.info("Hello Account Microservice");
    return "Get Account Infomation from Account Microservice";
  }
  
  @RequestMapping("/dealaccount.do")
  public String openAccount(String phonenumber) throws InterruptedException {
    int radom=RandomUtils.nextInt(10);
   
    String content="<openaccount><returncode>0</returncode><isnewaccount>"+(radom>4?"1":"0")+"</isnewaccount></openaccount>";
    log.info("openAccount:"+phonenumber+":"+content);
    return content;
  }

  @Configuration
  public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
    }
  }
}
